package com.apkplug.mediaplayerdemo;
import java.io.File;  
import java.io.IOException;  
import android.app.Activity;  
import android.media.AudioManager;  
import android.media.MediaPlayer;  
import android.os.Bundle;  
import android.os.Environment;  
import android.util.Log;  
import android.view.SurfaceHolder;  
import android.view.SurfaceView;  
import android.view.View;  
import android.widget.Button;
import android.widget.EditText;  
import android.widget.ImageButton;  
import android.widget.Toast;  
  
public class PlayerActivity extends Activity {  
    private static final String TAG = "PlayerActivity";  
    private EditText filenameText;  
    private MediaPlayer mediaPlayer;  
    private SurfaceView surfaceView;  
    private String filename;  
    private int position;  
      
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
          
        filenameText = (EditText)this.findViewById(R.id.filename);  
          
        surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);  
        surfaceView.getHolder().setFixedSize(176, 144);
        surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);  
        surfaceView.getHolder().addCallback(new SurfaceCallback());  
  
        mediaPlayer = new MediaPlayer();  
        ButtonClickListener listener = new ButtonClickListener();  
        Button playButton = (Button)this.findViewById(R.id.play);  
        Button pauseButton = (Button)this.findViewById(R.id.pause);  
        Button resetButton = (Button)this.findViewById(R.id.reset);  
        Button stopButton = (Button) this.findViewById(R.id.stop);  
        playButton.setOnClickListener(listener);  
        pauseButton.setOnClickListener(listener);  
        resetButton.setOnClickListener(listener);  
        stopButton.setOnClickListener(listener);  
    }  
      
    private final class SurfaceCallback implements SurfaceHolder.Callback{  
        @Override  
        public void surfaceCreated(SurfaceHolder holder) {  
            if(position>0 && filename!=null){  
                try {  
                    play();  
                    mediaPlayer.seekTo(position);  
                    position = 0;  
                } catch (IOException e) {  
                    Log.e(TAG, e.toString());  
                }  
            }  
        }  
        @Override  
        public void surfaceChanged(SurfaceHolder holder, int format, int width,  
                int height) {  
        }  
        @Override  
        public void surfaceDestroyed(SurfaceHolder holder) {  
            if(mediaPlayer.isPlaying()){  
                position = mediaPlayer.getCurrentPosition();  
                mediaPlayer.stop();  
            }  
        }         
    }  
  
    private final class ButtonClickListener implements View.OnClickListener{  
        @Override  
        public void onClick(View v) {  
          
            filename = filenameText.getText().toString();  
            try {  
                switch (v.getId()) {  
                case R.id.play:  
                    play();                   
                    break;  
  
                case R.id.pause:  
                    if(mediaPlayer.isPlaying()){  
                        mediaPlayer.pause();  
                    }else{  
                        mediaPlayer.start();  
                    }  
                    break;  
                case R.id.reset:  
                    if(mediaPlayer.isPlaying()){  
                        mediaPlayer.seekTo(0);  
                    }else{  
                        play();  
                    }  
                    break;  
                case R.id.stop:  
                    if(mediaPlayer.isPlaying()) mediaPlayer.stop();  
                    break;  
                }  
            } catch (Exception e) {  
                Log.e(TAG, e.toString());  
            }  
        }  
    }  
  
    private void play() throws IOException {  
        //File videoFile = new File(Environment.getExternalStorageDirectory(), filename);  
        mediaPlayer.reset();  
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);  
     
        mediaPlayer.setDisplay(surfaceView.getHolder());  
        mediaPlayer.setDataSource("http://streaming-http.icartoons.cn:7389/cmstest/20120923/129587/201208243200101258/W704.3gp?msisdn=45815707&spid=001&sid=01qz&appid=dm_qz_2413459173&trackid=1389949116047882171872d4c689d887---&animeversion=h");  
        mediaPlayer.prepare();     
        mediaPlayer.start();
    }  
}  